﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaArtAggregator.Core.com.msn.search.soap;
using MediaArtAggregator.Core.Configuration;

namespace MediaArtAggregator.Core.SearchProvider {
  public class LiveSearchSource : SearchSource {
    public override void Search ( string search, Settings settings, SearchType searchType ) {
      this.OnSearchInitialize ( EventArgs.Empty );
      string appid = "0A259117A06525B6093896EE13C6EA6EE11C75DC";
      MSNSearchService searchService = new MSNSearchService ( );
      SourceRequest sr = new SourceRequest ( );
      sr.Count = 100;
      sr.Source = SourceType.Image;
      sr.ResultFields = ResultFieldMask.Image;
      SearchRequest request = new SearchRequest();
      request.SafeSearch = SafeSearchOptions.Off;
      request.AppID = appid;
      request.Query = string.Format ( "{0} {1}", search, string.IsNullOrEmpty(searchType.Value) ? "" : searchType.Value );
      request.Requests = new SourceRequest[ ] { sr };
      request.CultureInfo = "en-US";

      SearchResponse response = searchService.Search ( request );
      foreach ( Result result in response.Responses[0].Results ) {
        string imageUrl = result.Image.ImageURL;
        if ( !string.IsNullOrEmpty ( imageUrl ) )
          this.Results.Add ( new Uri ( imageUrl ) );
      }
      this.OnSearchComplete ( EventArgs.Empty );
    }

    public override string ToString ( ) {
      return "images.live.com";
    }
  }
}
